import { _decorator, Component, Node, Prefab, UITransform, instantiate, Vec3, tween, CCFloat } from 'cc';
import {api} from "db://assets/script/game/player/api";
import {oops} from "db://oops-framework/core/Oops";
import {UIID} from "db://assets/script/game/common/config/gameUiConfig";
import {CategoryCellView} from "./categoryCellView";
import {GameItemView} from "./gameItemView";
import {pb} from "db://assets/script/network/ws/proto";
import {smc} from "db://assets/script/game/common/singletonModuleComp";
const { ccclass, property } = _decorator;

@ccclass('RudyDeskPanel')
export class RudyDeskPanel extends Component {
    @property({
        type: Prefab,
        tooltip: 'game item'
    })
    gameItem: Prefab;

    @property({
        type: Node,
        tooltip: '添加item 的 panel'
    })
    itemPanel: Node;

    @property({
        type: [Node],
        tooltip: '所有场次'
    })
    categoryList: Node[] = [];

    @property({
        type: Node,
        tooltip: '所有场次的根节点'
    })
    categoryRoot: Node;

    @property({
        type: CCFloat,
        tooltip: '载入动作的时长'
    })
    moveCategorySeconds: number;

    // 所有游戏列表
    gameItemList: any[]
    // 所有游戏场次
    gameItemCategoryList: any[]
    onEnter: any
    start() {
        console.log("start public desk panel")
        this.gameItemList = []
        this.gameItemCategoryList = []
        api.gameRubyDeskCategory({}, (resp: pb.IGameRubyDeskCategoryInfo[]) => {
            for (let i = 0; i < resp.length; i++) {
                const item = resp[i]
                const node = this.addGameItem(i, item.gameType, item.gameName)
                this.gameItemList.push({gameType: item.gameType, node})
                this.gameItemCategoryList.push(item.list)
            }
            this.onEnter()
        })
    }

    // TODO 支持不同游戏类型
    addGameItem(index: number, gameType: string, gameName: string) {
        const oneCell = instantiate(this.gameItem);
        const cellSize = oneCell.getComponent(UITransform)
        oneCell.parent = this.itemPanel
        oneCell.position = new Vec3(0, -cellSize.height * index - cellSize.height * cellSize.anchorY, 0)
        const gameItemView = oneCell.getComponent(GameItemView)
        gameItemView.setOnClickCallback(this.onGameItemClick.bind(this), index)
        gameItemView.setMajiang()
        gameItemView.noLine()
        gameItemView.noSelected()
        gameItemView.updateGameName(gameName)
        return oneCell
    }

    onGameItemClick(index: number) {
        this.addCategoryItem(this.gameItemList[index].gameType, this.gameItemCategoryList[index])
    }

    // 添加场次
    addCategoryItem(gameType: string, list: pb.ICategoryItem[]) {
        // 重置位置
        tween(this.categoryRoot).stop()
        this.categoryRoot.setPosition(new Vec3(2000, 30, 0))
        for (let i = 0 ; i < this.categoryList.length; i++) {
            const item = list[i]
            if (!item) {
                // 没有这个场次, 隐藏
                this.categoryList[i].active = false
                continue
            }
            const categoryView = this.categoryList[i].getComponent(CategoryCellView)
            categoryView.setConf(item, gameType)
            categoryView.updateView()
        }
        // 移动到150, 30
        tween(this.categoryRoot).to(this.moveCategorySeconds, {
            position: new Vec3(150, 30, 0)
        }).start()
    }

    // 返回大厅
    backDating() {
        smc.dating.show()
        oops.gui.remove(UIID.selectRubyDesk)
    }

    setOnEnter(gameType: string) {
        this.onEnter = () => {
            for (let i = 0; i < this.gameItemList.length; i++) {
                if (this.gameItemList[i].gameType === gameType) {
                    // 好了，就是它了，点击
                    const gameItemView = this.gameItemList[i].node.getComponent(GameItemView)
                    gameItemView.onClick(null)
                }
            }
        }
    }
}


